﻿public class Solution {
    public int MaxSubArray(int[] nums)
    {
        int[] dp = new int[nums.Length];
        dp[0] = nums[0];

        int res = dp[0];
        for (int i = 1; i < nums.Length; i++)
        {
            dp[i] = Math.Max(dp[i - 1] + nums[i], nums[i]);
            if (dp[i] > res) res = dp[i];
        }

        return res;
    }

    public static void Main(string[] args)
    {
        int[] nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
        Solution solution = new Solution();
        int res = solution.MaxSubArray(nums);
        Console.WriteLine(res);
    }
}